/**
 * 功能:对于sqllite增删改以及查询操作
 * 创建日期: 2012-03-16
 * 创建人: 全轩
 * 功能修正说明:
 * 		
 */

if(!this.DAL)
{
	this.DAL = {};
}

(function(){
	if(!DAL.dbname)
	{
		//数据库名称
		DAL.dbname = 'sina_app_mobile.db';
	}
	
	if(!DAL.currentdb)
	{
		//获取当前应用程序的数据库
		DAL.currentdb = {};
	}
	
	//初始化表对象
	if(typeof DAL.Init !== 'function')
	{
		DAL.Init = function(tablesinit)
		{
			for(var i=0;i<tablesinit.length;i++)
			{
				try{
					DAL.currentdb.execute(tablesinit[i].initSQL);
				}
				catch(e){
				}
			}
		}
	}
	
	//查询表对象
	if(typeof DAL.Query !== 'function')
	{
		DAL.Query = function(sql,columns)
		{
			DAL.currentdb = Titanium.Database.open(DAL.dbname);
			
			var array = [];
		    var rows = DAL.currentdb.execute(sql);
		    
		    /***********************************
		     * 拼凑js语句,把数据库中的数据放置到js数组中
		   ***********************************/
		    var exectext = '';
		    exectext = 'array.push({';
		    
		    for(var column = 0;column< columns.length;column++)
		    {
		    	exectext += columns[column] + ": rows.fieldByName('" + columns[column] + "'),";
		    }
		    
		    exectext = exectext.substring(0,exectext.length-1);
		    
	        exectext += '});'; 
	        /*
	         * 拼接结束
	         */
		        
		    while (rows.isValidRow()) { 
		    	eval(exectext); 
		        rows.next();  
		    }
		    
		    rows.close();
		    DAL.currentdb.close();
		    
		    return array;
		}
	}
	
	//删除表数据
	if(typeof DAL.Del !== 'function')
	{
		DAL.Del = function(tablename,where)
		{
			DAL.currentdb = Titanium.Database.open(DAL.dbname);
			var deletesql = '';
			
			if(where != null && where != '' && where != undefined && where != 'undefined'){
				deletesql = 'delete from ' + tablename + ' where ' + where;
			}
			else{
				deletesql = 'delete from ' + tablename;
			}

			var rows = DAL.currentdb.execute(deletesql);

		    DAL.currentdb.close();
		}
	}
	
	//添加表数据
	if(typeof DAL.Add !== 'function')
	{
		DAL.Add  = function(tablename,data,columns)
		{
			DAL.currentdb = Titanium.Database.open(DAL.dbname);
			var sql = 'insert into ' + tablename + '(';
			var sqlvalues = '';
			for(var item = 0; item< columns.length;item++)
			{
				sql +=  columns[item] + ',';
	       	}
	       	sql = sql.substring(0,sql.length -1);
	       	sql += ') values (';
	       	
	       	for(var item=0;item<data.length;item++)
	       	{
	       		sqlvalues = sql;
	       		for(var columnname = 0 ; columnname < columns.length;columnname++)
				{
					if(columns[columnname]=='tweet_text')
					{
						var temp = eval('data['+item + '].text');
						if(typeof temp == 'string')
						{
							temp = temp.replace("'","''");
						}
						else if(typeof temp == 'undefined')
						{
							temp = '';
						}
						else if(typeof temp == 'object')
						{
							temp = JSON.stringify(temp);
						}
						sqlvalues += "'" + temp + "',";
					}
					else if(columns[columnname]=='user' || columns[columnname] =='annotations')
					{
						var temp = JSON.stringify(eval('data['+item + '].' + columns[columnname]));
						if(typeof temp == 'string')
						{
							temp = temp.replace("'","''");
						}
						else if(typeof temp == 'undefined')
						{
							temp = '';
						}
						else if(typeof temp == 'object')
						{
							temp = JSON.stringify(temp);
						}
						sqlvalues += "'" + temp + "',";
					}
					else
					{
						var temp = eval('data['+item + '].' + columns[columnname]);
						
						if(typeof temp == 'string')
						{
							temp = temp.replace("'","''");
						}
						else if(typeof temp == 'undefined')
						{
							temp = '';
						}
						else if(typeof temp == 'object')
						{
							temp = JSON.stringify(temp);
						}
						sqlvalues += "'" + temp + "',";
					}
				}
				sqlvalues = sqlvalues.substring(0,sqlvalues.length-1);
				sqlvalues += ')';

	       		DAL.currentdb.execute(sqlvalues);
	       	}
	       	
	       	DAL.currentdb.close();
		}
	}
	
	if(typeof DAL.Update !== 'function')
	{
		DAL.Update = function(updatesql)
		{
			DAL.currentdb = Titanium.Database.open(DAL.dbname);
			
			DAL.currentdb.execute(updatesql);
			DAL.currentdb.close();
		}
	}
	
	DAL.currentdb = Titanium.Database.open(DAL.dbname);
	
	//初始化系统表
	DAL.Init(DataBase.dbInitSQL);
}())

//SinaApp.DAL = DAL;
	